<?php require_once 'conf.php'; ?>
<?php include("includes/libs.php"); ?>
<?php
			$userlist = array();

			$con = mysql_connect(GlobalConfig::db_host_name,GlobalConfig::db_username,GlobalConfig::db_password);
			if (!$con)
			{
				die('Could not connect: ' . mysql_error());
			}
			
			mysql_select_db(GlobalConfig::db_name, $con);
			
			$result = mysql_query("select id from user where active is true");
			while($row = mysql_fetch_array($result))
			{
				$id =  $row['id'];
				array_push($userlist, $id);
			}
			
			
			foreach($userlist as $userid){
		
		 		require_once "classes.php";
		 		require_once "services/budgetServices.php";
				$service = new CommonServices;
				$budgetServices = new BudgetService;
					 
				$con = mysql_connect(GlobalConfig::db_host_name,GlobalConfig::db_username,GlobalConfig::db_password);
				if (!$con)
				  {
				  die('Could not connect: ' . mysql_error());
				  }
				
				mysql_select_db(GlobalConfig::db_name, $con);
				
				$result = mysql_query("select * from account where userid = ". $userid);
				
				$equity = 0;
				
				while($row = mysql_fetch_array($result))
				{
					$balance =  $service->getBalanceForAcccount($row['id']);
					$equity = $equity + $balance;
				}
				
				$conn = new PDO(GlobalConfig::db_pdo_connect_string, GlobalConfig::db_username, GlobalConfig::db_password);
				$stmt = $conn->prepare("insert into equity_history (date, amount, userid) values (NOW(), :amount, :userid)");
				$stmt->bindParam(':amount', $equity);
				$stmt->bindParam(':userid', $userid);
				$stmt->execute();
				echo "userid: ". $userid." Equity = ".$equity."<br/>";
			}
?>